<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.username" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
    <view class="row b-b">
      <text class="tit">手机号</text>
      <input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" />
    </view>
    <view class="row b-b">
      <text class="tit">性别</text>
      <u-radio-group v-model="addressData.sex" active-color="#2aac34">
        <u-radio :name="0">男</u-radio>
        <u-radio :name="1">女</u-radio>
      </u-radio-group>    </view>
    <view class="row b-b">
      <text class="tit">出生年份</text>
      <span class="date-input" disabled v-html="addressData.year || '请选择年'" type="text" @click="show1 = true" placeholder="请选择年" ></span>
      <span class="delete-input" @click="addressData.year = ''">
        <u-icon color="#2aac34" size="36" name="close-circle"></u-icon>
      </span>
    </view>
    <view class="row b-b">
      <text class="tit">出生月份</text>
      <span class="date-input"  v-html="addressData.month || '请选择月'" type="text" @click="show2 = true" placeholder="请选择月"></span>
      <span class="delete-input" @click="addressData.month = ''">
        <u-icon color="#2aac34" size="36" name="close-circle"></u-icon>
      </span>
    </view>
    <view class="row b-b">
      <text class="tit">出生日</text>
      <span class="date-input"  v-html="addressData.day || '请选择日'" type="text" @click="show3 = true" placeholder="请选择日"></span>
      <span class="delete-input" @click="addressData.day = ''">
        <u-icon color="#2aac34" size="36" name="close-circle"></u-icon>
      </span>
    </view>
    <view class="row b-b">
      <text class="tit">出生时间</text>
      <span class="date-input"  v-html="addressData.hour || '请选择时间'" type="text" @click="show4 = true" placeholder="请选择时间"></span>
      <span class="delete-input" @click="addressData.hour = ''">
        <u-icon color="#2aac34" size="36" name="close-circle"></u-icon>
      </span>
    </view>
		<view class="row b-b">
			<text class="tit">地址</text>
      <input class="input" type="text" v-model="addressData.address" placeholder="详细地址" placeholder-class="placeholder" />
		</view>
<!--    <u-alert-tips :show-icon="true" type="warning" description="如知晓，编写时请注意使用英文逗号分隔，如不知晓，请输入出生日期系统计算"></u-alert-tips>-->
    <view class="row b-b" v-if="addressData.bazi">
      <text class="tit">八字</text>
      <input class="input" type="text" v-model="addressData.bazi" disabled placeholder-class="placeholder" />
    </view>
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.is_default" color="#2aac34" @change="switchChange" />
		</view>
    <button class="add-btn" @click="confirm">提交</button>
    <u-picker mode="time" v-model="show1" :params="{
					year: true,
    }" @confirm="confirm1"></u-picker>
    <u-picker mode="time" v-model="show2" :params="{
					month: true,
    }" @confirm="confirm2"></u-picker>
    <u-picker mode="time" v-model="show3" :params="{
					day: true,
    }" @confirm="confirm3"></u-picker>
    <u-picker mode="time" v-model="show4" :params="{
					hour: true,
    }" @confirm="confirm4"></u-picker>

	</view>
</template>

<script>
import UniDatetimePicker from '@/components/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue'

export default {
		components: {
			UniDatetimePicker
		},
		data() {
			return {
				addressData: {
          sex: 0,
          username: '',
          birthday: '',
					phone: '',
					address: '',
					code: '',
          is_default: false,
          year: '',
          month: '',
          day: '',
          hour: '',
          bazi: ''
				},
        show1: false,
        show2: false,
        show3: false,
        show4: false,
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
        this.$api.addressDetail({
          id: option.id
        }).then(res => {
          console.log(res)
          this.initData(res)
        })
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
      initData(res) {
        this.addressData = {
          ...res,
          is_default: res.is_default === 0
        }
      },
      confirm1(e) {
        this.addressData.year = e.year
      },
      confirm2(e) {
        this.addressData.month = e.month
      },
      confirm3(e) {
        this.addressData.day = e.day
      },
      confirm4(e) {
        this.addressData.hour = e.hour
      },
			switchChange(e){
				this.addressData.is_default = e.detail.value;
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
					}
				})
			},
			
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.username){
					this.$u.toast('请填写收货人姓名');
					return;
				}

				if(!data.address){
					this.$u.toast('请填写详细地址');
					return;
				}
		const formData = {
            ...data,
            is_default: data.is_default ? 0 : 1,
            member_id: uni.getStorageSync('userInfo').id,
          }
		  console.log(formData)
        if (this.addressData.id) {
          this.$api.updateAddress(formData).then(res => {
            this.$u.toast('操作成功');
            setTimeout(()=>{
              uni.navigateBack()
            }, 500)
          })
        } else {
          this.$api.addAddress(formData).then(res => {
            this.$u.toast('操作成功');
            setTimeout(()=>{
              uni.navigateBack()
            }, 500)
          })
        }

			},
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}
  .date-input{
    display: inline-block;
    width: 220rpx;
    font-size: 26rpx;
    text-align: left;
    padding: 0 10rpx;
  }
	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
    margin-bottom: 10rpx;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
      margin-right: 20rpx;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: #2aac34;
		border-radius: 10upx;
	}
</style>
